#include "PrintDlg.h"
#include "resource.h"
#include <iostream>
#include <ctime>
#include "MenuDlg.h"
#include "ValidateCardDlg.h"
#include "common.h"
#include "AccountBussiness.h"

using namespace std;

LRESULT CALLBACK CPrintDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
	CPrintDlg *Obj;
	if (message==WM_INITDIALOG)
	{	
		Obj=(CPrintDlg*)lParam;
		SetWindowLong(hWnd,GWL_USERDATA,(LONG)Obj);
	}
	Obj=(CPrintDlg*)GetWindowLong(hWnd,GWL_USERDATA);
	if (Obj)
		return Obj->DialogProc(hWnd, message, wParam, lParam);
	return 0;
}

CPrintDlg::CPrintDlg(HINSTANCE hInstance, HWND hParent, long lAmount, int type)
{
    this->lAmount = lAmount;
    this->type = type;
	DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_PRINT), hParent, (DLGPROC)CPrintDlgProc, (LPARAM)this);
}

CPrintDlg::~CPrintDlg(void)
{
	EndDialog(m_hDlg,0);
}

LRESULT CPrintDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		{
            AccountBussiness objAccBuss;
			m_hDlg=hWnd;
			SYSTEMTIME *date = new SYSTEMTIME;
            char* pBalance;
            char sBalance[15];
			char cdate[30];
			HWND hStaticDate = GetDlgItem(m_hDlg,IDC_STATIC_DATE);
			HWND hStaticAccountNo = GetDlgItem(m_hDlg,IDC_STATIC_ACCOUNT_NO);
			HWND hStaticCardNo = GetDlgItem(m_hDlg,IDC_STATIC_CARD_NO);
			HWND hStaticBalanceNo = GetDlgItem(m_hDlg,IDC_STATIC_BALANCE_NO);
			HWND hStaticTranMoney = GetDlgItem(m_hDlg,IDC_STATIC_TRANS_MONEY);
			HWND hStaticTranNo = GetDlgItem(m_hDlg,IDC_STATIC_TRANS_NO);

			GetLocalTime(date);
			sprintf_s(cdate,"%d:%d:%d  %d/%d/%d",date->wHour,date->wMinute,date->wSecond,date->wDay,date->wMonth,date->wYear);
			SetWindowText(hStaticDate,TEXT(cdate));
			SetWindowText(hStaticAccountNo,TEXT(g_Account.GetAccountID()));
			SetWindowText(hStaticCardNo,TEXT(g_Card.GetCardID()));
            
            pBalance = objAccBuss.ConvertToString(g_Account.GetBalance());
            sprintf_s(sBalance, "%s VND", pBalance);
			SetWindowText(hStaticBalanceNo, sBalance);
			
             if (this->type == 0) 
             {
                 ShowWindow(hStaticTranMoney,SW_HIDE);
                 ShowWindow(hStaticTranNo, SW_HIDE);
             }
             else 
            {
                 pBalance = objAccBuss.ConvertToString(this->lAmount);
                 sprintf_s(sBalance, "%s VND", pBalance);                 
                 SetWindowText(hStaticTranNo, sBalance); 
            }

			break;
		}
		
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_BUTTON_CLOSE:	
            if (this->type == 0) 
            {
                int MsgBoxID;
                MsgBoxID=MessageBox(hWnd,TEXT(CONTINOUS_TRANSACTION_TEXT),TEXT(CONTINOUS_TRANSACTION_CAPTION),MB_YESNO|MB_ICONQUESTION);
                if (MsgBoxID == IDYES)
                    {
                        EndDialog(hWnd,0);
                        MenuDlg objMenuDlg(hGlobalInst,hWnd);						
                        break;
                    }
                else
                    {
                        EndDialog(hWnd,0);
                        CValidateCardDlg objValidateCard(hGlobalInst,hWnd);						
                        break;
                    }
            }
            else
            {
                EndDialog(hWnd,0);
                CValidateCardDlg objValidateCard(hGlobalInst,hWnd);						
                break;
            }
		}
	default:
		return FALSE;
	}
	return FALSE;
}